﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TARLib.Messenger
{
    public class Messaggio
    {
        public Messaggio(Guid? messaggioID, string sender, string receiver, string testoMessaggio, bool isDelivered, bool isRead, DateTime timeStamp_Insert)
        {
            MessaggioID = messaggioID;
            Sender = sender;
            Receiver = receiver;
            TestoMessaggio = testoMessaggio;
            IsDelivered = isDelivered;
            IsRead = isRead;
            TimeStamp_Insert = timeStamp_Insert;
        }

        public Guid? MessaggioID { get; set; }
        public string Sender { get; set; }
        public string Receiver { get; set; }
        public string TestoMessaggio { get; set; }
        public bool IsDelivered { get; set; }
        public bool IsRead { get; set; }
        public DateTime TimeStamp_Insert { get; set; }

        public string TestoRaw
        {
            get
            {
                return String.Format("{0} ({1:HH:mm})", TestoMessaggio, TimeStamp_Insert);
            }
        }

        public void Send()
        {
            // Salvataggio su DB
            using (SqlConnection connection = new SqlConnection(Database.GetDBConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = "[Messenger].[SP_Messaggio_I]";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@MessaggioID", System.Data.SqlDbType.UniqueIdentifier) { Direction = System.Data.ParameterDirection.Output });

                cmd.Parameters.Add(new SqlParameter("@Sender", System.Data.SqlDbType.NVarChar, 50) { Value = Sender });
                cmd.Parameters.Add(new SqlParameter("@Receiver", System.Data.SqlDbType.NVarChar, 50) { Value = Receiver });

                cmd.Parameters.Add(new SqlParameter("@TestoMessaggio", System.Data.SqlDbType.NVarChar, 8000) { Value = TestoMessaggio });

                cmd.Parameters.Add(new SqlParameter("@IsDelivered", System.Data.SqlDbType.Bit) { Value = IsDelivered });
                cmd.Parameters.Add(new SqlParameter("@IsRead", System.Data.SqlDbType.Bit) { Value = IsRead });
                cmd.Parameters.Add(new SqlParameter("@TimeStamp_Insert", System.Data.SqlDbType.DateTime) { Value = TimeStamp_Insert });


                try
                {
                    connection.Open();
                    cmd.ExecuteNonQuery();
                    MessaggioID = new Guid(cmd.Parameters["@MessaggioID"].Value.ToString());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                    cmd.Dispose();
                }
            }
        }

        public void NotificaDelivery()
        {
            using (SqlConnection connection = new SqlConnection(Database.GetDBConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = "[Messenger].[SP_NotificaDelivery]";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@MessaggioID", System.Data.SqlDbType.UniqueIdentifier) { Value = MessaggioID });
                
                try
                {
                    connection.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                    cmd.Dispose();
                }
            }
        }

        public void NotificaLettura()
        {
            using (SqlConnection connection = new SqlConnection(Database.GetDBConnectionString()))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = "[Messenger].[SP_NotificaLettura]";
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@MessaggioID", System.Data.SqlDbType.UniqueIdentifier) { Value = MessaggioID });

                try
                {
                    connection.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                    cmd.Dispose();
                }
            }
        }
    }
}
